/*
 * Copyright FUOC.  All rights reserved.

 */
package managedbean;

import java.io.Serializable;
import java.util.Map;
import java.util.Properties;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.naming.Context;
import javax.naming.InitialContext;

import jpa.OrderJPA;
import ejb.OrderFacadeRemote;

/**
 * Managed Bean ShowPetMBean
 */
@ManagedBean(name = "findorder")
@SessionScoped
public class FindOrdersByIdMBean implements Serializable{
	
	private static final long serialVersionUID = 1L;	
	
	@EJB
	
	private OrderFacadeRemote orderFR;
	private OrderJPA order;
	private int idOrder;
	boolean consult = false;
	boolean list=false;
		
	public FindOrdersByIdMBean() throws Exception 
	{
		super();
	}
	public FindOrdersByIdMBean(int orderId) throws Exception 
	{
		setIdOrder(orderId);
		FindOrder();
	}
	
	public OrderJPA getOrder() {
		return order;
	}

	public int getIdOrder() {
		return idOrder;
	}

	public void setIdOrder(int idOrder) throws Exception {
		list=true;
		this.idOrder=idOrder;
		FindOrder();
	}
	
	public void setIdOrderConsult(int idOrder) throws Exception {
		consult=true;
		this.idOrder=idOrder;
		FindOrder();
	}
	
	public String ListOrder(){
	try {
		FacesContext fc = FacesContext.getCurrentInstance();
		int orderId = getOrderId(fc);
		setIdOrderConsult(orderId);
		return "FindOrderView";
	}
	catch (Exception e) {
		e.printStackTrace();
		return "ErrorView";
	}
}
	
	public int getOrderId(FacesContext fc){
 		Map<String,String> params = fc.getExternalContext().getRequestParameterMap();
 		return (Integer.parseInt(params.get("idOrder")));
 	}
	
	public boolean isConsult() {
		return consult;
	}
	public boolean isList() {
		return list;
	}
	@SuppressWarnings("unchecked")
	public void FindOrder() throws Exception
	{	
		Properties props = System.getProperties();
		Context ctx = new InitialContext(props);
		orderFR = (OrderFacadeRemote) ctx.lookup("java:app/PracticalCaseStudyJEE.jar/OrderFacadeBean!ejb.OrderFacadeRemote");
		order = (OrderJPA)orderFR.findOrdersById(idOrder);
	}

		
	
}
